export default {
    name: 'ChangeScret',
    data() {
        // 供应商编码
        let validateUserName = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入供应商号码'));
            } else {
                callback();
            }
        };
        // 供应商密码

        let validatePassword = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入供应商密码'));
            } else {
                callback();
            }
        };
        // 新密码

        let validateNewpassword = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入供应商新密码'));
            } else {
                callback();
            }
        };
        // 新密码确认

        let validateRepeatnewpassword = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入供应商新密码'));
            } else if (value !== this.accountForm.newPassword) {
                callback(new Error('两次输入密码不一致'));
            } else {
                callback();
            }
        };
        return {
            accontRules: {
                // 供应商手机号
                username: [{ validator: validateUserName, trigger: 'blur' }],
                // 供应商密码
                oldPassword: [{ validator: validatePassword, trigger: 'blur' }],
                // 新密码
                newPassword: [{ validator: validateNewpassword, trigger: 'blur' }],
                // 确认新密码
                repeatnewpassword: [{ validator: validateRepeatnewpassword, trigger: 'blur' }]
            },
            accountForm: {
                // 供应商手机号
                username: '',
                // 供应商旧密码
                oldPassword: '',
                // 新密码
                newPassword: '',
                // 确认新密码
                repeatnewpassword: ''
            }
        };
    },
    methods: {
        // 密码修改
        async submitAccontForm(accountForm) {
            this.$refs[accountForm].validate(async valid => {
                if (valid) {
                    this.$httpExt()
                        .post('/api/pmsSupplierAccount/changePassword', {
                            oldPassword: this.$generateRsaCode(this.accountForm.oldPassword),
                            newPassword: this.$generateRsaCode(this.accountForm.newPassword)
                        })
                        .then(
                            response => {
                                if (response.code === 200) {
                                    console.log('修改成功');
                                }
                                this.$message({
                                    message: '修改成功',
                                    type: 'success'
                                });
                            },
                            response => {
                                Notification.error({
                                    title: '异常',
                                    message: response.msg
                                });
                            }
                        );
                } else {
                    console.log('验证失败');
                }
            });
        }
    }
};
